﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

public partial class AddGroup : System.Web.UI.Page
{
    MySqlDataAdapter da;
    DataSet ds = new DataSet();
    MySqlCommand cmd = new MySqlCommand();
    MySqlConnection con;

    //private static string connectionString = "server=localhost;User Id=root;Password=root;database=workorganizer";
    static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginName"] == null || Session["UserId"] == null)
        {
            Response.Redirect("Home.aspx");
        }
        else
        {
            if (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) == 1)
            {
                if (!IsPostBack)
                {
                    // write logic to retrieve reporting to.
                    BindData();
                }
            }
            else
            {
                FormsAuthentication.SignOut();
                Session["LoginName"] = "";
                Session["UserType"] = "";
                Session["MobileNumber"] = "";
                Session["EmailId"] = "";
                Response.Redirect("Home.aspx");
            }
        }
    }

    public void BindData()
    {
        con = new MySqlConnection(connectionString);
        cmd.CommandText = "Select UserTypeId,UserType from UserTypes";
        cmd.Connection = con;
        da = new MySqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        ddlReportingTo.DataSource = ds.Tables[0];
        ddlReportingTo.DataTextField = "UserType";
        ddlReportingTo.DataValueField = "UserTypeId";
        ddlReportingTo.DataBind();
        con.Close();
    }

    protected void btnCreateGroup_Click(object sender, EventArgs e)
    {
        try
        {
            UserType ut = new UserType();
            ut.userType = this.tbGroupName.Text;
            ut.createdDate = DateTime.Now;
            ut.desc = this.tbGroupDesc.Text;
            ut.parent = this.ddlReportingTo.SelectedItem.Text;
            if (ut.parent.Equals(""))
                ut.level = 0;
            else if (ut.parent.ToLower().Contains("admin"))
                ut.level = 1;
            else if (ut.parent.ToLower().Contains("sales"))
                ut.level = 2;
            else if (ut.parent.ToLower().Contains("service") || ut.parent.ToLower().Contains("installation"))
                ut.level = 3;

            bool usertypeexists = EventDAO.checkUserTypeExists(ut.userType);

            if (usertypeexists == false)
            {
                int userTypeId = EventDAO.addUserType(ut);
                if (userTypeId != 0)
                {
                    Session["UserType"] = this.tbGroupName.Text;
                    Response.Redirect("AUVDGroups.aspx");
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('The group is already in our system. Please enter a different name.');", true);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}
